using System;
using System.Web.Mvc;
using System.Web.Security;
using NHibernate;
using RMH.DataAccess;
using RMH.Services;
using RMH.Web.Controllers;
using RMH.Web.RMHMembership;
using StructureMap;
using StructureMap.Attributes;

namespace RMH.Web
{
    public static class Container  
    {
        public static T Resolve<T>()
        {
            return ObjectFactory.GetInstance<T>();
        }

        public static T Resolve<T>(string key)
        {
            return ObjectFactory.GetNamedInstance<T>(key);
        }

        public static object Resolve(Type type)
        {
            return ObjectFactory.GetInstance(type);
        }


        static Container() { _Initialize(); }

        private static void _Initialize()
        {
            ObjectFactory.Initialize(i =>
            {
                i.ForRequestedType<IFormsAuthentication>()
                    .TheDefaultIsConcreteType<FormsAuthenticationService>();
                i.ForRequestedType<IMembershipService>()
                    .TheDefaultIsConcreteType<AccountMembershipService>();
                
                i.ForRequestedType<MembershipProvider>()
                    .TheDefault.Is
                    .ConstructedBy(x=>x.GetInstance<RMHMembershipProvider>());

                i.ForRequestedType<IPatientService>()
                    .TheDefaultIsConcreteType<PatientService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<IDiagnosisService>()
                    .TheDefaultIsConcreteType<DiagnosisService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<INoteService>()
                    .TheDefaultIsConcreteType<NoteService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<IRoomService>()
                    .TheDefaultIsConcreteType<RoomService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<ISupportPersonService>()
                    .TheDefaultIsConcreteType<SupportPersonService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<IStayService>()
                    .TheDefaultIsConcreteType<StayService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<ICountyService>()
                    .TheDefaultIsConcreteType<CountyService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<IStaffService>()
                    .TheDefaultIsConcreteType<StaffService>();
                i.ForRequestedType<IPaymentService>()
                    .TheDefaultIsConcreteType<PaymentService>()
                    .CacheBy(InstanceScope.Hybrid);
                i.ForRequestedType<ISessionFactoryFactory>()
                    .TheDefaultIsConcreteType<SessionFactoryFactory>()
                    .CacheBy(InstanceScope.Singleton);

                i.ForRequestedType<ISession>()
                    .TheDefault.Is.ConstructedBy(
                        c => c.GetInstance<ISessionFactoryFactory>().GetSession()
                    );

               
                    
//
//                i.ForRequestedType<IFormsAuthentication>()
//                    .TheDefaultIsConcreteType<FormsAuthenticationWrapper>();

                i.Scan(x =>
                {
                    x.Assembly("RMH.DataAccess");                    
                    x.AddAllTypesOf<IRepository>();                    
                    x.WithDefaultConventions();                       
                });

                i.Scan(x =>
                {
                    x.TheCallingAssembly();

                    x.AddAllTypesOf<IController>()
                        .NameBy(type => type.Name.Replace("Controller", ""));
                });

            });

        }

    }
}